package com.cloud.school.bus.teacherhelper.protocol.privateletter;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.view.View;

import com.android.support.jhf.utils.DateUtils;
import com.cloud.school.bus.teacherhelper.entitys.Letter;
import com.cloud.school.bus.teacherhelper.protocol.baseresponse.BaseJsonHttpResponse;

public class GetPrivateLetterListResponse extends BaseJsonHttpResponse {

	protected List<Letter> letterList = new ArrayList<Letter>();
	private String mLastAddTime = "-1";

	@Override
	public void onSuccess(int statusCode, Header[] headers,
			String responseString) throws Throwable {
		List<Letter> tempLetterList = new ArrayList<Letter>();
		if ("1".equals(code)) {
			JSONArray mainJsonArray = new JSONArray(responseString);
			for (int i = 0; i < mainJsonArray.length(); i++) {
				JSONObject mainJsonObject = mainJsonArray.optJSONObject(i);
				Letter letter = new Letter();
				/** 发布时间 */
				letter.addtime = mainJsonObject.optString("addtime", "");
				/** 私信内容 */
				letter.content = mainJsonObject.optString("content", "");
				/** 谁发的 */
				letter.from_role = mainJsonObject.optString("from_role", "");
				/** 发送者id */
				letter.from_uid = mainJsonObject.optString("from_uid", "");
				/** 私信类型txt;img */
				letter.letter_type = mainJsonObject.optString("letter_type", "");
				/** 私信id */
				letter.letterid = mainJsonObject.optString("letterid", "");
				/** 发给谁 */
				letter.to_role = mainJsonObject.optString("to_role", "");
				/** 接受者id */
				letter.to_uid = mainJsonObject.optString("to_uid", "");
				
				long addtime = 0L;
				try {
					addtime = Long.parseLong(letter.addtime) * 1000;
				} catch (Exception e) {

				}
				String date = DateUtils.dateFormat(addtime, "yyyy-MM-dd");
				letter.showDate = date;
//				if ("-1".equals(mLastAddTime) || !date.equals(mLastAddTime)) {
//					letter.showDate = date;
//					mLastAddTime = date;
//				}else {
//					letter.showDate = "";
//				}
				
				tempLetterList.add(letter);
			}
			
			for (int i = tempLetterList.size()-1; i >= 0; i--) {
				String date = tempLetterList.get(i).showDate;
				if ("-1".equals(mLastAddTime) || !date.equals(mLastAddTime)) {
					tempLetterList.get(i).isShowDate = true;
					mLastAddTime = date;
				} else {
					tempLetterList.get(i).isShowDate = false;
				}
				letterList.add(tempLetterList.get(i));
			}
			

		}
	}

}
